

/* prototype implementation*/
function cFormValidator(){}
//cFormValidator.prototype.constructor=function(){}
cFormValidator.prototype.inputs = [];
cFormValidator.prototype.inputs_types=[];

cFormValidator.prototype.errorclass = "alertInputError";
cFormValidator.prototype.inputsCount = cFormValidator.prototype.inputs.length;


cFormValidator.prototype.cleanErrors = function(){
		var elements = document.getElementsByClassName(this.errorclass);
 		for(var i = 0;i < elements.length;i++){
		elements[i].parentNode.removeChild(elements[i]);
		}
		
	}
cFormValidator.prototype.setForm = function(DomObj){
	this.form=DomObj;
}
cFormValidator.prototype.assignInput= function(domInput,input_type){
		if (!input_type) input_type='';
		//this.inputs.length++;
		var ii = this.inputs.length
		this.inputs[ii]=domInput;
		this.inputs_types[ii]=input_type;
	}
cFormValidator.prototype.validate=function(){
		this.cleanErrors();
		var _result = true;
		for (i=0;i<this.inputs.length;i++){
			var node=this.inputs[i];
			switch (this.inputs_types[i]){
			case "email":
			if (!this.validate_email(node.value)){
			this.alertDiv(node,"не верный email");
			_result=false;
			}
				break;
			default:
			if (!this.checkValue(node)){
			this.alertDiv(node);
			_result=false;
			}
			}
			
		}
	return _result;
	}

cFormValidator.prototype.validate_email = function (email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     return reg.test(email);
}

cFormValidator.prototype.unassignInputs =function(){
		this.inputs.length = 0;
	}
cFormValidator.prototype.blankErrors = function(){
	
	}
cFormValidator.prototype.checkValue = function (DomObj){
		
		
	 switch (DomObj.type){
		
		case "select-one": 
			if (DomObj.value==""){return false;}
			break;
		case "textarea":
		case "text":
			if (DomObj.value==""){return false;}
			break;
		//if (DomObj.value==""){return false;}
	 }
		//alert(DomObj.type);
		return true;
	}
cFormValidator.prototype.alertDiv = function(DomObj,txt){
		//alert("test");
		if (!txt) txt='обязательное поле';
		var domSPAN = document.createElement("span");
		domSPAN.className=this.errorclass;
		domSPAN.style.color="red"
		domSPAN.innerHTML=txt;

		DomObj.parentNode.insertBefore(domSPAN,DomObj.nextSibling);
		//DomObj.parentNode.insertBefore(domSPAN,DomObj.lastChild);
}

 var FormValidator= new cFormValidator();

//TestFormValidator = new cFormValidator();
// TestFormValidator.assignInput(document.getElementById('service_to_date'));
// onsubmit="if (!TestFormValidator.validate()) return false;"